package com.sdgakj.service.manager;

import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.persistence.DynamicSpecifications;
import org.springside.modules.persistence.SearchFilter;
import org.springside.modules.persistence.SearchFilter.Operator;

import com.google.common.collect.Maps;
import com.sdgakj.entity.Role;
import com.sdgakj.enums.EnumIsDelete;
import com.sdgakj.repository.RoleDao;


@Component
@Transactional
public class RoleService {
	
	@Autowired
	private RoleDao roleDao;
	
	private static Logger logger =LoggerFactory.getLogger(RoleService.class);
	
	/**
	 * 根据id查询权限
	 * @param id
	 * @return
	 */
	public Role getRoleById(Long id) {
		// TODO Auto-generated method stub
		return roleDao.findOne(id);
	}
	
	/**
	 * 保存Role
	 * @param entity
	 */
	public void saveRole(Role entity){
		roleDao.save(entity);
	}
	
	/**
	 * 查询列表
	 * @param userType
	 * @param searchParams
	 * @param pageNumber
	 * @param pageSize
	 * @param sortType
	 * @return
	 */
	public Page<Role> getRoles(Map<String,Object> searchParams,int pageNumber,int pageSize,
									String sortType){
		PageRequest pageRequest =buildPageRequest(pageNumber,pageSize,sortType);
		Specification<Role> spec =buildSpecification(searchParams);
		
		return roleDao.findAll(spec,pageRequest);
	}
	
	public List<Role> getAllRoles(){
		Map<String, SearchFilter> filters = Maps.newHashMap();
		filters.put("deltag", new SearchFilter("deltag", Operator.EQ, EnumIsDelete.NOTDELETE.getCode()));
		Specification<Role> spec = DynamicSpecifications.bySearchFilter(filters.values(), Role.class);
		Sort sort = buildSortRequest("id","ASC");
		return roleDao.findAll(spec, sort);

	}
	
	/**
	 * 更新权限信息
	 * @param entity
	 * @return 
	 */
	public Role updateRole(Role entity){
		return roleDao.save(entity);
	}	
	
	/**
	 * ajax请求根据操作级别获取permission
	 * @param type 按钮级别，如主菜单，子菜单，按钮
	 * @param pid  所属主菜单id
	 * @return
	 */
	public List<Role> getRoleByName(String rolename){
		Map<String, SearchFilter> filters = Maps.newHashMap();
		filters.put("deltag", new SearchFilter("deltag", Operator.EQ, EnumIsDelete.NOTDELETE.getCode())); //未删除
		if(StringUtils.isNotBlank(rolename)){
			filters.put("rolename", new SearchFilter("rolename", Operator.EQ, rolename)); //父节点
		}

		Specification<Role> spec = DynamicSpecifications.bySearchFilter(filters.values(), Role.class);
		return roleDao.findAll(spec);
	}
	/**
	 * 根据角色名称查询单个角色
	 * @param rolename
	 * @return
	 */
	public Role getRoleByRoleName(String rolename){
		Map<String, SearchFilter> filters = Maps.newHashMap();
		if(StringUtils.isNotBlank(rolename)){
			filters.put("rolename", new SearchFilter("rolename", Operator.EQ, rolename)); //父节点
		}

		Specification<Role> spec = DynamicSpecifications.bySearchFilter(filters.values(), Role.class);
		return roleDao.findOne(spec);
	}
	
	/**
	 * 创建排序.
	 */
	private Sort buildSortRequest(String sortName,String sortType) {
		Sort sort = null;
		if ("auto".equals(sortType)) {
			sort = new Sort(Direction.DESC, "id");
		}else if("desc".equals(sortType)){
			sort = new Sort(Direction.DESC, sortName);
		}else{
			sort = new Sort(Direction.ASC, sortName);
		}
		return sort;
	}
	
	/**
	 * 创建分页请求.
	 */
	private PageRequest buildPageRequest(int pageNumber, int pagzSize, String sortType) {
		Sort sort = null;
		if ("auto".equals(sortType)) {
			sort = new Sort(Direction.DESC, "id");
		}

		return new PageRequest(pageNumber - 1, pagzSize, sort);
	}
	
	/**
	 * 创建动态查询条件组合.
	 */
	private Specification<Role> buildSpecification(Map<String, Object> searchParams) {
		Map<String, SearchFilter> filters = SearchFilter.parse(searchParams);
		//  根据用户类型设置查询条件
//		filters.put("user.type", new SearchFilter("user.type", Operator.EQ, EnumUserType.CUSTOMOR.getCode()));
		Specification<Role> spec = DynamicSpecifications.bySearchFilter(filters.values(),Role.class);
		return spec;
	}
}
